package com.github.fujianlian.klinechart.draw.main;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.fujianlian.klinechart.BaseKLineChartView;
import com.github.fujianlian.klinechart.draw.Status;
import com.github.fujianlian.klinechart.entity.h.ICandle;
import com.github.fujianlian.klinechart.entity.h.IKLine;

/**
 * reate by tao on{DATE}
 */
public class MaDraw extends BaseMainDraw {


    int ma5Color;
    int ma30Color;
    int ma10Color;
    int ma60Color;
    
    private Paint ma5Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint ma10Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint ma30Paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint ma60Paint = new Paint(Paint.ANTI_ALIAS_FLAG);


    public MaDraw(BaseKLineChartView view) {
        super(view,Status.MA.getId());
    }
 

    @Override
    public void setLine(boolean line) {
        isLine = line;
    }

    @Override
    public void drawTranslated(@Nullable ICandle lastPoint, @NonNull ICandle curPoint, float lastX, float curX, @NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position) {
        if (!isStatus(Status.MA)) return;
        if (isLine()) {
            //画ma10
            if (lastPoint.getMa().getMA10Price() != 0) {
                view.drawMainLine(canvas, ma10Paint, lastX, lastPoint.getMa().getMA10Price(), curX, curPoint.getMa().getMA10Price());
            }
            //画ma30
            if (lastPoint.getMa().getMA30Price() != 0) {
                view.drawMainLine(canvas, ma30Paint, lastX, lastPoint.getMa().getMA30Price(), curX, curPoint.getMa().getMA30Price());
            }
            //画ma60
            if (lastPoint.getMa().getMA60Price() != 0) {
                view.drawMainLine(canvas, ma60Paint, lastX, lastPoint.getMa().getMA60Price(), curX, curPoint.getMa().getMA60Price());
            }
        } else {
            //画ma5
            if (lastPoint.getMa().getMA5Price() != 0) {
                view.drawMainLine(canvas, ma5Paint, lastX, lastPoint.getMa().getMA5Price(), curX, curPoint.getMa().getMA5Price());
            }
            //画ma10
            if (lastPoint.getMa().getMA10Price() != 0) {
                view.drawMainLine(canvas, ma10Paint, lastX, lastPoint.getMa().getMA10Price(), curX, curPoint.getMa().getMA10Price());
            }
            //画ma30
            if (lastPoint.getMa().getMA30Price() != 0) {
                view.drawMainLine(canvas, ma30Paint, lastX, lastPoint.getMa().getMA30Price(), curX, curPoint.getMa().getMA30Price());
            }
        }
    }



    @Override
    public void drawText(@NonNull Canvas canvas, @NonNull BaseKLineChartView view, int position, float x, float y) {
        ICandle point = (IKLine) view.getItem(position);
        if (!isStatus(Status.MA)) return;

        if (isLine){
            String text = "";
            if (point.getMa().getMA10Price() != 0) {
                text = "MA10:" + view.formatValue(point.getMa().getMA10Price()) + "  ";
                canvas.drawText(text, x, y, ma10Paint);
                x += ma10Paint.measureText(text);
            }
            if (point.getMa().getMA20Price() != 0) {
                text = "MA30:" + view.formatValue(point.getMa().getMA30Price());
                canvas.drawText(text, x, y, ma10Paint);
            }
            if (point.getMa().getMA60Price() != 0) {
                text = "MA60:" + view.formatValue(point.getMa().getMA60Price()) + "  ";
                canvas.drawText(text, x, y, ma10Paint);
            }
        }else {
            String text = "";
            if (point.getMa().getMA5Price() != 0) {
                text = "MA5:" + view.formatValue(point.getMa().getMA5Price()) + "  ";
                canvas.drawText(text, x, y, ma10Paint);
                x += ma5Paint.measureText(text);
            }
            if (point.getMa().getMA10Price() != 0) {
                text = "MA10:" + view.formatValue(point.getMa().getMA10Price()) + "  ";
                canvas.drawText(text, x, y, ma10Paint);
                x += ma10Paint.measureText(text);
            }
            if (point.getMa().getMA20Price() != 0) {
                text = "MA30:" + view.formatValue(point.getMa().getMA30Price());
                canvas.drawText(text, x, y, ma10Paint);
            }
        }
        
    }
 
    public void setMa5Color(int color) {
        ma5Color=color;
        ma5Paint.setColor(color);
    }
 
    public void setMa10Color(int color) {
        ma10Color=color;

        ma10Paint.setColor(color);
    }
 
    public void setMa30Color(int color) {
        ma30Color=color;
        ma60Color=color;
        ma30Paint.setColor(color);
    }

    public void setStrokeWidth(float width) {
        ma5Paint.setStrokeWidth(width);
        ma10Paint.setStrokeWidth(width);
        ma30Paint.setStrokeWidth(width);
    }
    

    public void setTextSize(float textSize) {
        ma10Paint.setTextSize(textSize);
    }

    @Override
    public void changgeColor(boolean changeColor) {
        ma5Paint.setColor(changeColor  ? Color.GRAY : ma5Color);
        ma10Paint.setColor(changeColor  ? Color.GRAY :ma10Color );
        ma30Paint.setColor(changeColor  ? Color.GRAY :ma30Color );
        ma60Paint.setColor(changeColor  ? Color.GRAY : ma60Color);
    }
}
